<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>期约</title>
</head>

<body>
  <script>
    // 以往的异步编程
    // function double(val) {
    //   setTimeout(() => {
    //     setTimeout(console.log, 0, val * 2)
    //   }, 1000);
    // }
    // let r1 = double(3)

    // function double(val, callback) {
    //   setTimeout(() => { callback(val * 2) }, 1000)
    // }

    // double(3, (x) => console.log(`I was given ${x}`))

    // 异步成功/失败处理
    // function double(val, success, failure) {
    //   setTimeout(() => {
    //     try {
    //       if (typeof val !== 'number') {
    //         throw `输入的第一个参数必须是'number'类型!`
    //       }
    //       success(2 * val)
    //     } catch (error) {
    //       failure(error)
    //     }
    //   }, 1000);
    // }

    // const successCallback = x => console.log(`success:${x}`);
    // const failureCallback = e => console.log(`failure:${e}`);

    // double(3, successCallback, failureCallback)
    // double('b', successCallback, failureCallback)

    // 期约promise
    // let p = new Promise((resolve, reject) => setTimeout(resolve, 1000))
    // setTimeout(console.log, 0, p)
    // console.log(p);

    // try {
    //   Promise.reject(new Error('bar'))
    // } catch (error) {
    //   console.log(error);
    // }

    // let p1 = Promise.reject('foo')
    // let p2 = p1.then()
    // setTimeout(console.log, 0, p2)
    // let p3 = p1.then(null, () => undefined)
    // setTimeout(console.log, 0, p3);


    // let p1 = Promise.resolve()
    // let p2 = p1.finally(() => new Promise(() => { }))
    // setTimeout(console.log, 0, p1)
    // setTimeout(console.log, 0, p2)

    // let p1 = Promise.resolve('foo')
    // let p2 = p1.finally(() => new Promise((resolve, reject) => setTimeout(() => {
    //   resolve('bar')
    // }, 1000)))

    // setTimeout(console.log, 0, p2)
    // setTimeout(console.log, 2000, p2)
    // setTimeout(() => {
    //   setTimeout(console.log, 0, p2)
    // }, 2000);

    // let synchronousResolve
    // let p = new Promise((resolve) => {

    //   console.log('1:invoking resolve()')
    //   resolve()
    //   console.log('2:resolve() returns')

    // })

    // p.then(() => console.log('4:then() handler executes'))

    // // synchronousResolve()
    // console.log('3:synchronousResolve() return');


    // 期约链
    // let p1 = new Promise((resolve, reject) => {
    //   console.log('first');
    //   setTimeout(resolve
    //     , 1000);
    // })

    // let p2 = p1.then(() => new Promise((resolve, reject) => {
    //   console.log('second')
    //   setTimeout(resolve, 1000);
    // }))
    //   .then(() => new Promise((resolve, reject) => {
    //     console.log('third')
    //     setTimeout(resolve, 1000);
    //   }))
    //   .then(() => new Promise((resolve, reject) => {
    //     console.log('fourth')
    //     setTimeout(resolve, 1000);
    //   }))

    // let p = new Promise((resolve, reject) => {
    //   console.log('initial promise rejects');
    //   reject();
    // })

    // let p2 = p.catch(() => console.log('reject handler'))
    //   .then(() => console.log('resolve handler'))
    //   .catch(() => console.log('reject handler'))
    //   .finally(() => console.log('finally handler'))

    // let p1 = Promise.all([Promise.resolve(), Promise.reject()])

    function addTwo(x) { return x + 2 }
    function addThree(x) { return x + 3 }
    function addFive(x) { return x + 5 }

    function addTen(x) {
      return [addTwo, addThree, addFive].reduce((promise, fn) => promise.then(fn), Promise.resolve(x))
    }

    addTen(8).then(console.log)

    const res = [{ a: 1, b: 2 }, 5, 8, 9, { c: 3, d: 4 }].reduce((prev, cur) => { prev + cur }, 0)
    console.log(res);
  </script>
</body>

</html>
